package com.example.aevovein.ui.skilltree;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.aevovein.databinding.ItemSkillBinding;
import com.example.aevovein.model.Skill;

import java.util.List;

public class SkilltreeAdapter extends RecyclerView.Adapter<SkilltreeAdapter.ViewHolder> {
    private List<Skill> skills;
    private final Context context;
    private final SkillUpdateListener skillUpdateListener;

    public SkilltreeAdapter(Context context, List<Skill> skills, SkillUpdateListener skillUpdateListener) {
        this.context = context;
        this.skills = skills;
        this.skillUpdateListener = skillUpdateListener;
    }

    public List<Skill> getSkills() {
        return skills;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemSkillBinding binding = ItemSkillBinding.inflate(
                LayoutInflater.from(parent.getContext()), parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Skill skill = skills.get(position);
        holder.titleView.setText(String.format("%s (%d/100)", skill.getName(), skill.getLevel()));
        holder.descriptionView.setText(skill.getDescription());
        holder.increaseButton.setOnClickListener(v -> {
            skillUpdateListener.onSkillUpdated(skill, 1); // 增加技能等级
        });
        holder.decreaseButton.setOnClickListener(v -> {
            skillUpdateListener.onSkillUpdated(skill, -1); // 减少技能等级
        });
        
        // 添加删除按钮点击事件
        holder.deleteButton.setOnClickListener(v -> {
            skillUpdateListener.onSkillDeleted(skill);
        });
    }

    @Override
    public int getItemCount() {
        return skills.size();
    }

    // 更新技能数据
    public void updateSkills(List<Skill> newSkills) {
        this.skills = newSkills;
        notifyDataSetChanged(); // 刷新适配器
    }

    // ViewHolder 类，用于绑定布局
    static class ViewHolder extends RecyclerView.ViewHolder {
        final TextView titleView;
        final TextView descriptionView;
        final Button increaseButton;
        final Button decreaseButton;
        final ImageButton deleteButton;

        ViewHolder(ItemSkillBinding binding) {
            super(binding.getRoot());
            titleView = binding.skillTitle;
            descriptionView = binding.skillDescription;
            increaseButton = binding.btnIncrease;
            decreaseButton = binding.btnDecrease;
            deleteButton = binding.btnDelete;
        }
    }

    // 定义接口，用于技能更新的回调
    public interface SkillUpdateListener {
        void onSkillUpdated(Skill skill, int delta); // delta: +1 增加，-1 减少
        void onSkillDeleted(Skill skill); // 新增删除技能的回调
    }
}